package org.tis.tools.operatelog.module.log.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.LogOperate;
import org.tis.tools.model.log.vo.LogOperateDetail;
import org.tis.tools.operatelog.module.log.service.ILogOperateService;
import org.tis.tools.starter.mybatisplus.querykit.model.Query;

import javax.validation.constraints.NotBlank;

/**
 * 操作日志
 *
 * @author chenchao
 * @author zhaochunhai
 *
 * Created on 2018/6/1 18:03
 */
@RestController
@RequestMapping("/v1/api/logOperate")
@Validated
public class LogOperateController extends BaseController<LogOperate> {

    @Autowired
    private ILogOperateService logOperateService;

    @ApiOperation(value = "分页查询操作日志",notes = "传分页数据(page),筛选条件(condition)作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated Query<LogOperate> query) {
        return ResultVO.success("查询成功", logOperateService.selectPage(query.getPageObj(),
                query.getWrapper()));
    }

    @ApiOperation(value = "根据GUID查询操作日志",notes = "传入guid作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "guid不能为空") String id) {
        LogOperate logAbfOperate = logOperateService.selectById(id);
        if (logAbfOperate == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        return ResultVO.success("查询成功", logAbfOperate);
    }

    @ApiOperation(value = "根据操作员分页查询操作日志",notes = "传入分页数据(page),操作员(userId)作为参数")
    @PostMapping("/operator")
    public ResultVO listByUser(@RequestBody @Validated Query<LogOperate> query) {
        return ResultVO.success("查询成功", logOperateService.queryListByUser(query.getPageObj(),
                query.getWrapper()));
    }

    @ApiOperation(value = "新增操作日志",notes = "传入操作日志信息作为参数")
    @PostMapping
    public ResultVO add(@RequestBody @Validated LogOperateDetail logOperateDetail) {
        logOperateService.insertOperatorLog(logOperateDetail);
        return ResultVO.success("新增成功");
    }

}
